using System;
using System.Collections;
using System.Collections.Generic;
using FairyGUI;
using FairyGUI.Utils;
using UnityEngine;

namespace UIPack
{
    public partial class ComAnswer : GComponent
    {
        private static ComAnswer ins;
        public static ComAnswer Instance
        {
            get
            {
                if (ins == null)
                {
                    ins = CreateInstance();
                    ins.Init();
                }
                return ins;
            }
        }

        
        public string[] Args;
        private float cd;
        private bool hasCd = false;
        private float cdTotal;

        public void Init()
        {
            onAddedToStage.Set(OnAdd);

        }
        

        /// <summary>
        /// 每帧调用的一个回调。如果你要override，请记住以下两点：
        /// 1、记得调用base.onUpdate;
        /// 2、不要在方法里进行任何会更改显示列表的操作，例如AddChild、RemoveChild、visible等。
        /// </summary>
        override protected void OnUpdate()
        {
            base.OnUpdate();

            if (hasCd) {
                cd -= Time.deltaTime;
                ImgProgress.fillAmount = cd / cdTotal;
                Txt.text = Helper.GetDHMSFormatCD(Convert.ToInt32(cd));
                if (cd <= 0) {

                    if (Args[1] != "-1") {
                        LoseHandle();
                    }
                    else {
                        //LvHelper.IsCurrQueueEventOver = true;
                    }

                    ImgProgress.fillAmount = 0;

                    hasCd = false;
                }
            }
        }

        /// <summary>
        /// 倒计时结束或者选错
        /// </summary>
        private void LoseHandle() {
            List<string[]> args = new List<string[]>();
            args.Add(new string[] {Args[3]});

            Pop.RemovePop(this);

            var list = new List<int>();
            list.Add(200);
            //LvHelper.RunEventQueue(list, args);
        }

        private void OnAdd() {
            List.RemoveChildrenToPool();
            var startIdx = 4;
            for (int i = startIdx; i < Args.Length; i++)
            {
                var item = List.AddItemFromPool() as ComAnswerItem;
                item.Txt.text = Args[i];
                item.onClick.Set(OnItemClick);
                item.data = (i - startIdx) + "";
            }

            cdTotal = cd = Convert.ToSingle(Args[0]); 
            hasCd = cd != -1;

            if (hasCd) {
                ImgProgress.visible = true;
                ImgProgress.fillAmount = 1;
                Txt.visible = true;
                Txt.text = Helper.GetDHMSFormatCD(Convert.ToInt32(cd));
            }
            else {
                ImgProgress.visible = false;
                Txt.visible = false;
            }

            TxtDes.text = Args[2];
        }
        private void OnItemClick(EventContext context)
        {
            var item = context.sender as ComAnswerItem;
            if (item.data.ToString() == Args[1])
            {
                //LvHelper.IsCurrQueueEventOver = true;
            }
            else {
                LoseHandle();
            }

            Pop.RemovePop(this);
        }

    }
}